package cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment4Refactoring.scoreList2.core;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment4Refactoring.bean.CourseScoreList;

public class CourseScoreListSerializer {
	public static Document serializeToDom(CourseScoreList list) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(CourseScoreList.class);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://jw.nju.edu.cn/schema ../ScoreList.xsd");
			// SchemaFactory sf =
			// SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			// Schema schema = sf.newSchema(new File("xml/StudentList.xsd"));
			// jaxbMarshaller.setSchema(schema);
			// ByteArrayOutputStream ba = new ByteArrayOutputStream();
			// jaxbMarshaller.marshal(list, ba);
			// System.out.println(ba.toString());

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(true);
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.newDocument();
			jaxbMarshaller.marshal(list, doc);
			return doc;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
